﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    public partial class FormNotepad : Form
    {
        bool b = false;//新旧
        bool s = true;//是否保存


        public FormNotepad()
        {
            InitializeComponent();

        }

     

        private void RtxtNotepad_TextChanged(object sender, EventArgs e)
        {
            s = false;
        }

        private void TsmiNew_Click(object sender, EventArgs e)
        {
            if (b == true || RtxtNotepad.Text.Trim() != "")
            {
                string result;
                result = MessageBox.Show("文件尚未保存，是否保存？",
                    "保存文件", MessageBoxButtons.YesNoCancel).ToString();

                switch (result)
                {
                    case "Yes":
                        if (b == true)
                        {
                            RtxtNotepad.SaveFile(ofdlgNotepad.FileName);
                        }
                        else if (sfdlgNotepad.ShowDialog() == DialogResult.OK)
                        {
                            RtxtNotepad.SaveFile(sfdlgNotepad.FileName);
                        }
                        s = true;
                        RtxtNotepad.Text = "";
                        break;

                    case "No":
                        b = false;
                        RtxtNotepad.Text = "";
                        break;
                }
            }
        }

        private void TsmiOpen_Click(object sender, EventArgs e)
        {
            if (b == true || RtxtNotepad.Text.Trim() != "")
            {
                if (s == false)
                {
                    string result;
                    result = MessageBox.Show("文件尚未保存，是否保存？",
                        "保存文件", MessageBoxButtons.YesNoCancel).ToString();

                    switch (result)
                    {
                        case "Yes":
                            if (b == true)
                            {
                                RtxtNotepad.SaveFile(ofdlgNotepad.FileName);
                            }
                            else if (sfdlgNotepad.ShowDialog() == DialogResult.OK)
                            {
                                RtxtNotepad.SaveFile(sfdlgNotepad.FileName);
                            }
                            s = true;

                            break;

                        case "No":
                            b = false;
                            RtxtNotepad.Text = "";
                            break;
                    }
                }
            }
            ofdlgNotepad.RestoreDirectory = true;
            if ((ofdlgNotepad.ShowDialog() == DialogResult.OK) &&
                ofdlgNotepad.FileName != "")
            {
                //打开代码语句
                RtxtNotepad.LoadFile(ofdlgNotepad.FileName);
                b = true;
            }
            s = true;
        }

        private void TsmiSave_Click(object sender, EventArgs e)
        {
            if (b == true && RtxtNotepad.Modified == true)
            {
                RtxtNotepad.SaveFile(ofdlgNotepad.FileName);
                s = true;
            }
            else if (b == false && RtxtNotepad.Text.Trim() != "" &&
                sfdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                RtxtNotepad.SaveFile(sfdlgNotepad.FileName);
                s = true;
                b = true;
                ofdlgNotepad.FileName = sfdlgNotepad.FileName;
            }
        }

        private void TsmiSaveAs_Click(object sender, EventArgs e)
        {
            if (sfdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                RtxtNotepad.SaveFile(sfdlgNotepad.FileName);
                s = true;
            }
        }

        private void TsmiClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void TsmiCopy_Click(object sender, EventArgs e)
        {
            RtxtNotepad.Copy();
        }
        private void TsmiUndo_Click(object sender, EventArgs e)
        {
            RtxtNotepad.Undo();
        }

        private void TsmiCut_Click(object sender, EventArgs e)
        {
            RtxtNotepad.Cut();
        }

        private void TsmiPaste_Click(object sender, EventArgs e)
        {
            RtxtNotepad.Paste();
        }

        private void TsmiSelectAll_Click(object sender, EventArgs e)
        {
            RtxtNotepad.SelectAll();
        }

        private void TsmiDate_Click(object sender, EventArgs e)
        {
            RtxtNotepad.AppendText(System.DateTime.Now.ToString());
        }

        private void TsmiAuto_Click(object sender, EventArgs e)
        {
            if (TsmiAuto.Checked == false)
            {
                TsmiAuto.Checked = true;
                RtxtNotepad.WordWrap = true;
            }
            else
            {
                TsmiAuto.Checked = false;
                RtxtNotepad.WordWrap = false;
            }

        }

        private void TsmiFont_Click(object sender, EventArgs e)
        {
            fdlgNotepad.ShowColor = true;
            if (fdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                RtxtNotepad.SelectionColor = fdlgNotepad.Color;
                RtxtNotepad.SelectionFont = fdlgNotepad.Font;
            }
        }

        private void TsmiToolStrip_Click(object sender, EventArgs e)
        {
            Point point;
            if (TsmiToolStrip.Checked == true)
            {
                point = new Point(0, 24);
                TsmiToolStrip.Checked = false;
                toolNotepad.Visible = false;
                RtxtNotepad.Location = point;
                RtxtNotepad.Height += toolNotepad.Height;
            }
            else
            {
                point = new Point(0, 49);
                TsmiToolStrip.Checked = true;
                toolNotepad.Visible = true;
                RtxtNotepad.Location = point;
                RtxtNotepad.Height -= toolNotepad.Height;
            }
        }

        private void TsmiStatusStrip_Click(object sender, EventArgs e)
        {
            if (TsmiStatusStrip.Checked == true)
            {
                TsmiStatusStrip.Checked = false;
                stsNotepad.Visible = false;
                RtxtNotepad.Height += stsNotepad.Height;
                //stsNotepad
            }
            else
            {
                TsmiStatusStrip.Checked = true;
                stsNotepad.Visible = true;
                RtxtNotepad.Height -= stsNotepad.Height;
            }
        }

        private void TsmiAbout_Click(object sender, EventArgs e)
        {
            frmAbout obFrmAbout = new frmAbout();
            obFrmAbout.Show();
        }

        private void toolNotepad_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int n;
            n = toolNotepad.Items.IndexOf(e.ClickedItem);
            switch (n)
            {
                case 0:
                    TsmiNew_Click(sender, e);
                    break;
                case 1:
                    TsmiOpen_Click(sender, e);
                    break;
                case 2:
                    TsmiSave_Click(sender, e);
                    break;
                case 5:
                    TsmiCut_Click(sender, e);
                    break;
                case 6:
                    TsmiCopy_Click(sender, e);
                    break;
                case 7:
                    TsmiPaste_Click(sender, e);
                    break;
                case 9:
                    TsmiAbout_Click(sender, e);
                    break;
            }
        }

        private void timerNotepad_Tick(object sender, EventArgs e)
        {

            tssLabel2.Text = System.DateTime.Now.ToString();
        }

        

        private void FormNotepad_SizeChanged(object sender, EventArgs e)
        {

            tssLabel1.Width = this.Width / 2 - 12;
            tssLabel2.Width = tssLabel1.Width;
        }
    }//class
}//namespace
